package com.easondongh.business.controller;

import com.easondongh.business.domain.Customer;
import com.easondongh.business.domain.Provider;
import com.easondongh.business.vo.CustomerQueryVo;
import com.easondongh.common.enums.ResponseEnum;
import com.easondongh.common.response.ResponseObj;
import com.easondongh.business.service.CustomerService;
import com.easondongh.system.utils.StringUtils;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.business.vo.CustomerQueryVo;
import com.easondongh.common.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author EasonDongH
 * @date 2020/5/26 8:17
 */
@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 新增客户
     * @param customer
     * @return
     */
    @PostMapping("/addCustomer")
    public ResponseObj addCustomer(@RequestBody Customer customer) {
        if (customer == null || StringUtils.isBlank(customer.getCustomerName()) || StringUtils.isBlank(customer.getConnectionPerson())
                || StringUtils.isBlank(customer.getPhone())) {
            log.warn("客户：新增时客户名称、联系人、联系电话均不能为空");
            return new ResponseObj(ResponseEnum.ADD_FAIL);
        }
        this.customerService.save(customer);
        return new ResponseObj(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 修改客户
     * @param customer
     * @return
     */
    @PutMapping("updateCustomer")
    public ResponseObj updateCustomer(@RequestBody Customer customer) {
        if (customer == null || StringUtils.isBlank(customer.getCustomerName()) || StringUtils.isBlank(customer.getConnectionPerson())
                || StringUtils.isBlank(customer.getPhone())) {
            log.warn("客户：更新时id、客户名称、联系人、联系电话均不能为空");
            return new ResponseObj(ResponseEnum.UPDATE_FAIL);
        }
        this.customerService.updateById(customer);
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 批量删除客户
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds")
    public ResponseObj deleteByIds(@RequestBody List<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            this.customerService.removeByIds(ids);
        }
        return new ResponseObj(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 分页查询客户
     * 用于后台查询，会将未启用的客户页查询出来
     * @return
     */
    @PostMapping("/getByPage")
    public ResponseObj<PageResult<Customer>> getByPage(@RequestBody BasePageQueryVo<CustomerQueryVo> queryCondition) {
        if(queryCondition == null) {
            return ResponseObj.QueryFail;
        }
        PageResult<Customer> result = this.customerService.getByPage(queryCondition);
        return new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, result);
    }

    /**
     * 启用当前客户
     * @param id
     * @return
     */
    @PutMapping("/enable")
    public ResponseObj enableById(Integer id) {
        if(id != null) {
            this.customerService.enableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 弃用当前客户
     * @param id
     * @return
     */
    @PutMapping("/disable")
    public ResponseObj disableById(Integer id) {
        if(id != null) {
            this.customerService.disableById(id);
        }
        return new ResponseObj(ResponseEnum.UPDATE_SUCCESS);
    }

    @GetMapping("/getAllEnableCustomer")
    public  ResponseObj<List<Customer>> getAllEnableProvider() {
        List<Customer> customers = this.customerService.getAllEnableCustomer();
        return  new ResponseObj<>(ResponseEnum.QUERY_SUCCESS, customers);
    }

}
